package com.pactera.madp.cp.controller.oms.workroom;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.entity.workroom.WorkRoomGroupEntity;
import com.pactera.madp.cp.service.impl.AccountService;
import com.pactera.madp.cp.service.IWorkRoomGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @description:客户群分组
 * @author: Huayu
 * @time: 2020/12/8 14:28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/workroomgroup")
@Api(value = "workroomgroup", tags = "客户群分组")
public class WorkRoomGroupController {

    private IWorkRoomGroupService workRoomGroupServiceImpl;

    /**
     *
     * @description: 客户群分组列表
     * @return: 
     * @author: Huayu
     * @time: 2020/12/8 17:07
     */
    @GetMapping("/page")
    @ApiOperation(value = "客户群分组列表")
    public R WorkRoomGroupIndex(Page page){
        Integer corpIds = AccountService.getCorpId();        //分页数据
        return workRoomGroupServiceImpl.getWorkRoomGroupList(String.valueOf(corpIds),page);
    }

    /**
     *
     * @description:删除
     * @return:
     * @author: Huayu
     * @time: 2020/12/9 8:48
     */
    @SysLog("删除客户群分组")
    @DeleteMapping("/{workRoomGroupId}")
    @ApiOperation(value = "删除客户群分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workRoomGroupId",value="客户群分组id",dataType ="Integer")
    })
    public R WorkRoomGroupDestroy(@PathVariable Integer workRoomGroupId){
        WorkRoomGroupEntity workRoomGroupEntity = workRoomGroupServiceImpl.getWorkRoomGroupById(workRoomGroupId);
        if(workRoomGroupEntity == null){
            throw  new CheckedException("该客户群分组不存在，不可操作");
        }
        //客户群分组表
        return R.ok(workRoomGroupServiceImpl.deleteWorkRoomGroup(Integer.valueOf(workRoomGroupId)));
    }

    /**
     * @description: 客户群分组管理-新建客户群分组提交.
     * @return: 
     * @author: Huayu
     * @time: 2020/12/9 8:57
     */
    @SysLog("创建客户群分组")
    @PostMapping
    @ApiOperation(value = "创建客户群分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="corpId",value="企业id",dataType ="Integer"),
            @ApiImplicitParam(name="workRoomGroupName",value="客户群分组名称",dataType ="String")
    })
    public R WorkRoomGroupStore(Integer corpId,String workRoomGroupName){
        //验证客户群分组名称是否已经存在
        WorkRoomGroupEntity workRoomGroupEntity = workRoomGroupServiceImpl.getWorkRoomGroupsByCorpId(corpId,workRoomGroupName);
        if(workRoomGroupEntity != null){
            throw new CheckedException("该客户群分组名称已存在，不可重复添加");
        }
        //数据入表
        WorkRoomGroupEntity workRoomGroupEntity1 = new WorkRoomGroupEntity();
        workRoomGroupEntity1.setCorpId(Integer.valueOf(corpId));
        workRoomGroupEntity1.setName(workRoomGroupName);
        workRoomGroupServiceImpl.createWorkRoomGroup(workRoomGroupEntity1);
        return R.ok();
    }

    /**
     *
     * @description: 客户群分组管理- 更新提交.
     * @return: 
     * @author: Huayu
     * @time: 2020/12/9 9:37
     */
    @SysLog("编辑客户群分组")
    @PutMapping
    @ApiOperation(value = "编辑客户群分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workRoomGroupId",value="客户群分组id",dataType ="Integer"),
            @ApiImplicitParam(name="workRoomGroupName",value="客户群分组名称",dataType ="String")
    })
    public R WorkRoomGroupUpdate(Integer workRoomGroupId,String workRoomGroupName){
        WorkRoomGroupEntity workRoomGroupEntity = workRoomGroupServiceImpl.getWorkRoomGroupById(workRoomGroupId);
        if(workRoomGroupEntity == null){
            throw new CheckedException("该客户群分组不存在，不可操作");
        }
        //验证当前企业下客户群分组名称是否重复
        WorkRoomGroupEntity workRoomGroupEntity1 = workRoomGroupServiceImpl.getWorkRoomGroupsByCorpId(workRoomGroupEntity.getCorpId(),workRoomGroupName);
        if(workRoomGroupEntity1 != null){
            throw new CheckedException("该客户群分组名称已存在，不可更新");
        }
        //数据入表
        workRoomGroupServiceImpl.updateWorkRoomGroupById(workRoomGroupId,workRoomGroupName);
        return R.ok();
    }

}
